package com.martinwj.camera2demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.FragmentTransaction;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.martinwj.camera2demo.module.CameraFragment;
import com.martinwj.camera2demo.utils.ImageUtils;
import com.martinwj.camera2demo.utils.PermissionDialog;
import com.martinwj.camera2demo.utils.PermissionUtils;

public class MainActivity extends AppCompatActivity {

    // 相机
    private CameraFragment mCameraFragment;
    private static final String SETTING_ACTION = "com.martinwj.camera2demo.setting";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setWindowFlag();
        setContentView(R.layout.activity_main);
    }

    /**
     * 设置Window flag
     */
    private void setWindowFlag() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        window.setAttributes(params);
    }

    @Override
    protected void onStart() {
        super.onStart();
        PermissionUtils.checkPermission(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (PermissionUtils.isPermissionGranted(this)) {
            initCameraFragment();
            if (isSettingShortcut()) {
//                addSettingFragment();
                getIntent().setAction(null);
            }
        }
    }

//    private void addSettingFragment() {
//        FragmentTransaction transaction = getFragmentManager().beginTransaction();
//        transaction.replace(R.id.app_root, new SettingFragment(),
//                SettingFragment.class.getSimpleName());
//        transaction.addToBackStack(SettingFragment.class.getSimpleName());
//        transaction.commit();
//    }

    private boolean isSettingShortcut() {
        return SETTING_ACTION.equals(getIntent().getAction());
    }

    private void initCameraFragment() {
        if (mCameraFragment == null) {
            mCameraFragment = new CameraFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.app_root, mCameraFragment);
            transaction.commit();
        }
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionUtils.REQUEST_CODE) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    showPermissionDenyDialog();
                    return;
                }
            }
        }
    }

    private void showPermissionDenyDialog() {
        PermissionDialog dialog = new PermissionDialog();
        dialog.show(getFragmentManager(), "PermissionDeny");
    }


}